package com.cloud.product.controller;

import com.cloud.dto.CartDto;
import com.cloud.product.entity.ProductCategory;
import com.cloud.product.entity.ProductInfo;
import com.cloud.product.service.ProductCategoryInf;
import com.cloud.product.service.ProductServiceInf;
import com.cloud.product.entity.ProductCategoryVO;
import com.cloud.product.entity.ProductInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * describe:产品接口
 *
 * @author zhanghaixuan
 * @date 2018/09/10
 **/
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductServiceInf productServiceInf;

    @Autowired
    private ProductCategoryInf productCategoryInf;

    @GetMapping("/list")
    public ResponseEntity list(){
        List<ProductInfo> upProductInfo =  productServiceInf.findUpProductInfo();

        List<Integer> categoryTypeList = upProductInfo.stream()
                 .map(ProductInfo::getCategoryType)
                 .collect(Collectors.toList());

        List<ProductCategory> byCategoryTypeIn =  productCategoryInf.findByCategoryTypeIn(categoryTypeList);

        List<ProductCategoryVO> returnList = new ArrayList<>();

        for (ProductCategory productCategory : byCategoryTypeIn) {
            ProductCategoryVO productCategoryVO = new ProductCategoryVO();

            productCategoryVO.setCategoryName(productCategory.getCategoryName());
            productCategoryVO.setCategoryType(productCategory.getCategoryType());

            List<ProductInfoVO> list = new ArrayList<>();

            for (ProductInfo productInfo : upProductInfo) {

                if(productInfo.getCategoryType().equals(productCategory.getCategoryType())){
                    ProductInfoVO productInfoVO = new ProductInfoVO();
                    productInfoVO.setProductId(productInfo.getProductId());
                    productInfoVO.setProductName(productInfo.getProductName());
                    productInfoVO.setProductPrice(productInfo.getProductPrice());
                    productInfoVO.setProductDescrption(productInfo.getProductDescription());
                    productInfoVO.setProductIcon(productInfo.getProductIcon());
                    list.add(productInfoVO);
                }
            }

            if(list.size()>0){
                productCategoryVO.setProductInfoVOList(list);
                returnList.add(productCategoryVO);
            }
        }

        return ResponseEntity.ok(returnList);
    }

    /**
     * 获取商品的列表
     * @param productList 商品Id列表
     * @return ResponseEntity
     */
    @PostMapping("/listForOrder")
    public ResponseEntity listForOrder(@RequestBody List<String> productList){
        return  ResponseEntity.ok(productServiceInf.findProductByIds(productList));
    }

    /**
     * 扣库存的操作
     * @param cartDtos 需要扣库存的信息
     */
    @PostMapping("/decreaseStock")
    public void decreaseStock(@RequestBody List<CartDto> cartDtos){
        productServiceInf.decreaseStock(cartDtos);
    }
}
